/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.figures;

import org.eclipse.draw2d.BorderLayout;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.Panel;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;

import com.cea.papyrus.diagraminterchange2.di2.GraphElement;

/**
 * @author Andreas Limyr
 * @author frankd@ifi.uio.no TODO Refactor - this class is basically identical to
 *         StateInvFigure
 */
public class GuardFigure extends InteractionFragmentFigure1 {
    private final Label label;
    private final Panel panel;

    // TODO Remove and calculate based on text
    // private static final Dimension DEFAULT_SIZE = new Dimension(13,13);

    public GuardFigure() {
        BorderLayout layout = new BorderLayout();
        setLayoutManager(layout);
        setBackgroundColor(ColorConstants.listBackground);
        // setBorder(new LineBorder());

        label = new Label();

        panel = new Panel();
        panel.setLayoutManager(new ToolbarLayout(ToolbarLayout.HORIZONTAL));
        panel.add(new Label("["));
        panel.add(label);
        panel.add(new Label("]"));

        add(panel, BorderLayout.CENTER);
    }

    // FIXME This method shouldn't be used, as the next method replaces it
    /*
     * public Rectangle getPreferredBounds(Rectangle suggestedBounds) { if
     * (suggestedBounds.width == 0) return new Rectangle(suggestedBounds.getLocation(),
     * DEFAULT_SIZE); else return suggestedBounds; //return suggestedBounds; //throw new
     * UnsupportedOperationException("Not used"); }
     */

    @Override
    public Rectangle getPreferredModelBounds(final Rectangle bounds, final Rectangle coveredBounds) {
        final Rectangle preferredBounds =
                new Rectangle(bounds.getLocation(), panel.getPreferredSize());
        return preferredBounds;
        // TODO enable lifeline centering:
        // return super.getPreferredModelBounds(preferredBounds, coveredBounds);
    }

    @Override
    public void validate() {
        if (isValid())
            return;

        Dimension prefSize = panel.getPreferredSize();
        if (!prefSize.equals(getSize()))
            setSize(prefSize);

        super.validate();
    }

    @Override
    protected boolean useLocalCoordinates() {
        return true;
    }

    // --- ILabeledFigure ---

    public Rectangle getLabelBounds() {
        Rectangle rect = label.getClientArea().getCopy();
        label.translateToAbsolute(rect);
        return rect;
    }

    public void setLabel(String labelText) {
        label.setText(labelText);
        invalidate();
        validate();
    }

    // -- Anchor contribution ---

    public int[] contributeAnchors() {
        return AnchorContributor.NO_CONTRIBUTIONS;
    }

    @Override
    public void refreshAll(GraphElement graphElement) {
        super.refreshAll(graphElement);
        this.panel.setBackgroundColor(this.getBackgroundColor());
        this.panel.setForegroundColor(this.getForegroundColor());
        this.label.setFont(this.getFont());
        label.setForegroundColor(this.getFontColor());
        this.repaint();
    }
}
